######################################################
# 合并数据
######################################################
# 合并数据集
# 粘贴数据结构
# 向量
x <- c("a", "b", "c")
y <- c("A", "B", "C")
paste(x, y)
paste(x, y, sep = "-")
paste(x, y, sep = "-", collapse = "&")

# 数据框/矩阵
name <- c("PHI", "NYM", "FLA", "ATL", "WSN")
age <- c(92, 89, 94, 72, 59)
stu <- data.frame(name, age)
sex <- c("f", "m", "f", "m", "m")
stu2 <- data.frame(name, age, sex)
stu <- cbind(stu, sex)
rbind(stu, stu2)

# 通过共同字段合并数据
name <- c("PHI", "NYM", "FLA", "ATL", "WSN")
age <- c(92, 89, 94, 72, 59)
sex <- c("f", "m", "f", "m", "m")
stu_age <- data.frame(name, age)
stu_sex <- data.frame(name2 = name, sex)
merge(stu_age, stu_sex)
merge(stu_age, stu_sex, by.x = "name", by.y = "name2")
name2 <- c("PHI", "NYM", "WSN")
age2 <- c(92, 89, 94)
stu_age2 <- data.frame(name2, age2)
merge(stu_age2, stu_sex, all.y = T)

######################################################
# 转换数据
######################################################

data <- read.csv(file = "C:/Users/LoveP/Desktop/data.csv", header = T)
data
class(data$出生日期)
data$出生日期 <- as.Date(data$出生日期)
data
math <- c(80,60)
chinese <- c(60,70)
score <- data.frame(数学 = math, 语文 = chinese)
data <- cbind(data, score)
data
# 变量重新赋值
data$平均成绩 <- (data$数学 + data$语文) / 2
data
# 转换函数transform
data.transform <- transform(data, 总成绩=data$数学 + data$语文)
data.transform

#########################
# apply系列函数
#########################
######### apply #########
m <- matrix(c(1:10), nrow=2)
m
apply(m, 1, sum)
apply(m, 2, sum)
apply(m, c(1, 2), sum)

x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
x
dimnames(x)[[1]] <- letters[1:8]
apply(x, 2, mean, trim = .2)  #2列
apply(x, 1, mean, trim = .2)  #1行
apply(x, 2, sort)

######### lapply #########
df <- data.frame(x=c(1:3),y=c(6:8))
lapply(df, mean)

######### sapply #########
x<-list(a= 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
#simplify = FALSE,同lapply 返回list对象
sapply(x, mean, simplify = FALSE, USE.NAMES = FALSE) #等同于lapply
sapply(x, mean, USE.NAMES = FALSE) #默认返回数组

######### vapply #########
#返回值的类型和长度要和预设值一致
x<-list(a= 1:10, beta = exp(-3:3), logic =c(TRUE,FALSE,FALSE,TRUE))
vapply(x, function(x) {c(mean(x),sd(x))}, FUN.VALUE = c(0, 0), USE.NAMES = TRUE)

######### plyr #########
# aaply adply alply a_ply
# daply ddply dlply d_ply
# laply ldply llply l_ply
d<-data.frame(x=1:5, y=6:10)
install.packages("plyr")
library("plyr")
llply(.data=d, .fun=function(x) 2^x)

data$年龄 <- llply(.data = data$出生日期,
                 .fun = function(x) as.integer((Sys.Date() - x)/365) )
data

######### tapply #########
height <- c(174, 165, 180, 171, 160)
sex<-c("F","F","M","F","M")
#返回vector
tapply(height, sex, mean)
#返回list
tapply(height, sex, mean, simplify = F)

# 交叉表
df<-data.frame(year=kronecker(2001:2003,rep(1,4)),
               loc=c('beijing','beijing','shanghai','shanghai'), 
               type=rep(c('A','B'),6), sale=rep(1:12))
#以年份为行、地区为列计算销售总量
tapply(df$sale, df[,c('year','loc')], sum)
#以年份为行，类别为列计算销售总量
tapply(df$sale, df[,c('year','type')], sum)
#以年份，地区和类别计算销售总量
tapply(df$sale, df[,c('year','loc','type')],sum)

######################################################
# 数据分段
######################################################
x=1:100
table(cut(x, breaks = 8))
# 正态分布
Z <- stats::rnorm(10000)
table(cut(Z, breaks = -6:6))
hist(Z, breaks = -6:6)
# 分组变量合并对象
hat.sizes <- seq(from=6.25, to=7.75, by=0.25)
shoe.sizes <- seq(from=7, to=12)
make.groups(hat.sizes, shoe.sizes)

sim.dat <-
  make.groups(uniform = runif(200),
              exponential = rexp(175),
              lognormal = rlnorm(150),
              normal = rnorm(125))
qqmath( ~ data | which, sim.dat, scales = list(y = "free"))


######################################################
# 子集
######################################################
data <- read.csv(file = "C:/Users/LoveP/Desktop/data.csv", header = T)
# 中括号索引的方式
data[data$性别=="男", c("姓名","性别","出生日期")]
# subset函数
subset(data, 性别=="男", c("姓名","性别","出生日期"))

# 随机抽样
data[sample(1:nrow(data), 3), ]
sample(1:10, 3)
sample(c("a", "b", "c", "d"),2)

######################################################
# 汇总函数
######################################################
# 交叉表tapply
df<-data.frame(year=kronecker(2001:2003,rep(1,4)),
               loc=c('beijing','beijing','shanghai','shanghai'), 
               type=rep(c('A','B'),6), sale=rep(1:12))
#以年份为行、地区为列计算销售总量
tapply(df$sale, df[,c('year','loc')], sum)
#以年份为行，类别为列计算销售总量
tapply(df$sale, df[,c('year','type')], sum)
#以年份，地区和类别计算销售总量
tapply(df$sale, df[,c('year','loc','type')],sum)

# by
by(df$sale, INDICES = list(df$year, df$type), FUN = sum)

# aggregate
aggregate(x=df[, c("sale")], by=list(df$year, df$type), FUN = sum)

# rowsum
rowsum(x=df[, c("sale")], group=df$year)

# 计数
tabulate(c(2,3,5))
tabulate(c(2,3,3,5), nbins = 10)
tabulate(df$type)

table(df$type)

xtabs(~year+type, df)

######################################################
# 数据修整
######################################################
# 转置
m <- matrix(1:10, nrow=5)
t(m)

df<-data.frame(year=kronecker(2001:2006,rep(1,2)),
               type=rep(c('A','B'),6), sale=rep(1:12))
unstacked <- unstack(df, form = sale~year)
stack(unstacked)
reshape(df, idvar = "year", timevar = "type", direction = "wide")

# reshape库
install.packages("reshape")
library("reshape")
melt(df)
cast(data=df, year~type)

df<-data.frame(year=kronecker(2001:2003,rep(1,4)),
               loc=c('beijing','beijing','shanghai','shanghai'), 
               type=rep(c('A','B'),6), sale=rep(1:12))
cast(data=df, year~loc|type)

######################################################
# 数据排序去重
######################################################
# 去重
df<-data.frame(year=kronecker(2001:2003,rep(1,4)), 
               type=rep(c('A','B'),6), sale=rep(1:2,6))
df
unique(df)
# 排序
sort(df$sale)
df
df[order(df$year,df$sale),]

######################################################
# 处理缺失值
######################################################
data <- read.csv(file = "C:/Users/LoveP/Desktop/data.csv", header = T)
data[is.na(data)] <- 0
data
is.na(data)
complete.cases(data)
sum(!complete.cases(data))
# 删除缺失行
data <- na.omit(data)